<?php

/**
 * winbinder 패키지 
 * 
 * 
 * @package php.ui.winbinder
 */

/**
 * WBContainer 
 *
 * @package php.ui.winbinder
 */
class WBContainer extends WBComponent { 
	private $_componentList = array();
	private $_layout = null;
	private $_margin = array();

	public function __construct($options = array()) { 
		parent::__construct($options);
	}
	
	public function __destruct() { 
		parent::__destruct();
		
		unset($this->_componentList);
		unset($this->_layout);
		unset($this->_margin);
	}		
	
	protected function initEvent() { 
		parent::initEvent();

		// 이벤트 설정 
		$this->setEvent('add', 'onAdd');
		$this->setEvent('remove', 'onRemove');
	}

	/**
	 * WBComponent 추가 
	 *
	 * @return WBComponent 추가된 component 
	 */
	public function add($com) { 

		$com = WBApplication::createObject($com, $this);

		if ($this->isVisible()) { 
			if ($com['autoVisible']) {
				$com->setVisible(true);
			}
		} else { 
			$com->setVisible(false);
		}
		
		$this->_componentList[] = $com;

		//$eventObject = new WBEvent(wbparent($this), $this, $com);
		
		//$this->fireEvent('add' , $eventObject);		

		// 레이아웃 다시 그림 
		$this->doLayout();

		return $com;
	}

	/**
	 * 컴포넌트 삭제 
	 *
	 */
	public function remove($com) { 
		$tempComponent = $com;

		if (is_string($tempComponent)) { 
			$tempComponent = WBApplication::getObject($tempComponent); 
		}

		$tempList = array();

		foreach ($this->_componentList as $c) { 
			if ($c == $tempComponent) continue;

			$tempList[] = $c;
		}

		//$eventObject = new WBEvent(wbparent($this), $this, $tempComponent);
		
		//$this->fireEvent('remove' , $eventObject);

		return $tempComponent;
	}

	/**
	 * component 리스트 얻어오기 
	 *
	 * @return array 
	 */
	public function getComponentList($isAll = false) {

		if ($isAll) { 
			return $this->_componentList;
		} else { 
			$temp = array();
			
			foreach ($this->_componentList as $com) {
				if ($com->isVisible()) $temp[] = $com;
			}
			
			return $temp;
		}
	}

	/**
	 * component 개수 얻어오기 
	 *
	 * @return int 
	 */
	public function length() { 
		return count($this->_componentList);
	}

	/**
	 * 특정 컴포넌트 얻어오기 
	 *
	 * @param int $i 컴포넌트 인덱스 
	 * @return WBComponent 
	 */
	public function get($i) { 
		return $this->_componentList[$i];
	}

	/**
	 * layout 설정 
	 *
	 */
	public function setLayout($layout = null) { 
		$this->_layout = WBApplication::createLayout($layout);
	}

	/**
	 * layout 얻어오기 
	 *
	 * @return WBLayout 
	 */
	public function getLayout() { 
		return $this->_layout;
	}

	/**
	 * Layout 실행 , 화면구조 변경 
	 *
	 */
	public function doLayout() { 
		if ($this->_layout != null) {
			// 레이아웃 실행 
			$this->_layout->render($this);
		}
	}

	/**
	 * 개별 여백 설정 하기 
	 *
	 * left, right, top, bottom 
	 */
	public function setMargin($direction = 'left', $margin) { 
		$this->_margin[$direction] = $margin;
	}

	/**
	 * 여백얻어오기 
	 *
	 * @return int 
	 */
	public function getMargin($direction) { 
		return $this->_margin[$direction];
	}

	/**
	 * 여백 전체 설정 
	 *
	 *
	 */
	public function setMarginAll($margin = array()) { 
		$arr = array('left','right','top','bottom');
		foreach ($arr as $direction) { 
			$this->setMargin($direction, $margin[$direction]);
		}
	}

	/**
	 * 전체 여백 얻어오기 
	 *
	 * @return array 
	 */
	public function getMarginAll() { 
		return $this->_margin;
	}


}

?>